﻿Imports myBookmarks.Core
Imports System.Data.Linq
Namespace myBookmarks.Data
    Public Class UserRepository
        Implements IUserRepository
        Private _db As BookmarkContext


        Public Sub New(ByVal db As BookmarkContext)
            _db = db
        End Sub

        Public Function GetByUsername(ByVal username As String) As User Implements IUserRepository.GetByUsername

            Dim user As User = (From u In _db.bm_Users _
                               Where u.UserName = username _
                               Select New User With {.UserId = u.UserId, .UserName = u.UserName, .FirstName = u.FirstName, _
                                                     .PasswordHash = u.PasswordHash, .PasswordSalt = u.PasswordSalt, _
                                                     .LastName = u.LastName, .CreatedDate = u.CreatedDate, .Email = u.Email, .LastVisited = u.LastVisited, _
                                                     .UserContacts = (From uc In _db.bm_UserContacts _
                                                                      Where uc.UserName = u.UserName _
                                                                      Select New UserContact With {.ContactId = uc.ContactId, .UserName = uc.UserName, .UserContact = uc.UserContacts}).ToList, _
                                                     .ResetPasswordToken = u.ResetPasswordToken}).FirstOrDefault
            Return user

        End Function

        Public Function CreateUser(ByVal user As User) As Boolean Implements IUserRepository.CreateUser
            Dim u As New bm_User
            u.UserId = user.UserId
            u.UserName = user.UserName
            u.FirstName = user.FirstName
            u.LastName = user.LastName
            u.Email = user.Email
            u.LastVisited = user.LastVisited
            u.CreatedDate = user.CreatedDate
            u.PasswordHash = user.PasswordHash
            u.PasswordSalt = user.PasswordSalt
            Try
                _db.bm_Users.InsertOnSubmit(u)
                _db.SubmitChanges()
                Return True
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        Public Function SaveUser(ByVal user As User) As Boolean Implements IUserRepository.SaveUser
            Try
                Dim u As bm_User = (From us In _db.bm_Users _
                           Where us.UserName = user.UserName _
                           Select us).FirstOrDefault
                u.UserId = user.UserId
                u.UserName = user.UserName
                u.FirstName = user.FirstName
                u.LastName = user.LastName
                u.Email = user.Email
                u.LastVisited = user.LastVisited
                u.CreatedDate = user.CreatedDate
                u.PasswordHash = user.PasswordHash
                u.PasswordSalt = user.PasswordSalt
                u.ResetPasswordToken = user.ResetPasswordToken

                For Each uc As UserContact In user.UserContacts
                    If uc.ContactId = Nothing Then
                        u.bm_UserContacts.Add(New bm_UserContact With {.UserContacts = uc.UserContact, .bm_User = u})
                    End If
                Next
                _db.SubmitChanges()
                Return True
            Catch ex As Exception
                Throw ex
            End Try
        End Function


    End Class
End Namespace